#include "shell_port.h"
#include "stdio.h"
#include "string.h"
#include "usbd_cdc_if.h"

Shell shell;
char shell_buffer[512];

//log日志的代码，实现日志控制和多色日志
#if SHELL_USING_CMD_LOG

#include "log.h"
#define true 1

void USB_LogWrite(char * buffer,short len);
Log usbLog = {
    .write = USB_LogWrite,
    .active = true,
    .level = LOG_DEBUG
};
void USB_LogWrite(char * buffer,short len)
{
    if(usbLog.shell)
    {
        shellWriteEndLine(usbLog.shell,buffer,len);
    }
}
#endif


//底层代码(USB_CDC接口相关的实现代码)

// shell接口文件
signed short USER_ShellWrite(char * data, unsigned short len)              
{
    uint16_t time_out = 60000;
    uint8_t res = 0;
	while((res = CDC_Transmit_FS((uint8_t *)data, (uint16_t)len)) && time_out)time_out--;
    // printf("res = %d\r\n",res);
    // printf("data = %s\r\n",data);
    return (short)len;
}


void User_ShellTask(void)
{ 
    if(usb_rx_len)
    {
        // printf("receive:%s\r\n",usb_rx_data);
        for(uint16_t i = 0; i < usb_rx_len ; i++)
            shellHandler(&shell, usb_rx_data[i]);
        usb_rx_len = 0;
    }
} 

void User_Shell_Init(void)
{
    // 初始化使能USB的部分
    shell.write = USER_ShellWrite; //挂上写函数
    shellInit(&shell,shell_buffer,512); //设置缓冲区
#ifdef SHELL_USING_CMD_LOG
     logRegister(&usbLog,&shell);
#endif
}









